package com.example.popmp3.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.popmp3.R;
import com.example.popmp3.dao.LoveListDAO;
import com.example.popmp3.dao.MusicListDAO;
import com.example.popmp3.dao.SingleListDAO;
import com.example.popmp3.model.Tb_lovelist;
import com.example.popmp3.model.Tb_singlelist;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2016/3/9.
 */
public class SingleList extends Activity {
    public static final String FLAG = "id";
    String idType="";
    String[] strinfos;
    ImageButton imageButton;
    TextView textView;
    Button button;
    ListView listView3;
    String loveid;
    SingleListDAO singleListDAO = new SingleListDAO(SingleList.this);
    LoveListDAO loveListDAO = new LoveListDAO(SingleList.this);
    MusicListDAO musicListDAO=new MusicListDAO(SingleList.this);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singlelist);

        imageButton=(ImageButton)findViewById(R.id.back_up);
        textView=(TextView)findViewById(R.id.textView1);
        button=(Button)findViewById(R.id.button1);
        listView3=(ListView)findViewById(R.id.listView3);
        Intent intent = getIntent();// 创建Intent对象
        Bundle bundle = intent.getExtras();// 获取便签id
        loveid = bundle.getString("id");// 将便签id转换为字符串
        textView.setText(loveListDAO.find(Integer.parseInt(loveid)).getName());
        ShowInfo(R.id.textView1);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SingleList.this, InMusic.class);
                intent.putExtra("id", loveid);
                startActivity(intent);
            }
        });

        listView3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String strInfo = String.valueOf(((TextView) view).getText());// 记录单击的项信息
                String strid = strInfo.substring(0, strInfo.indexOf(' '));// 从项信息中截取编号
                idType="1";
                String musicid =Integer.toString( singleListDAO.find(Integer.parseInt(strid)).getMusicid());
                Intent intent = new Intent(SingleList.this, PlayMusic.class);
                intent.putExtra("id", new String[] {musicid,idType});
                startActivity(intent);
            }
        });
        listView3.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, final long id) {

                AlertDialog alert = new AlertDialog.Builder(SingleList.this).create();
                alert.setTitle("Warning!");    //设置对话框的标题
                alert.setIcon(android.R.drawable.ic_dialog_info);
                alert.setMessage("Are You Sure To Delete?");
                String strInfo = String.valueOf(((TextView) view).getText());// 记录单击的项信息
                final String strid = strInfo.substring(0, strInfo.indexOf(' '));

                alert.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(SingleList.this, "您取消删除歌曲！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                //添加确定按钮
                alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        singleListDAO.detele(Integer.parseInt(strid));
                        Toast.makeText(SingleList.this, "删除歌曲成功！",
                                Toast.LENGTH_SHORT).show();
                    }
                });
                alert.show(); // 创建对话框并显示
                return true;
            }
        });
    }
    public void ShowInfo(int id) {
        String[] strInfos = null;// 定义字符串数组，用来存储信息
        ArrayAdapter<String> arrayAdapter =  null;// 创建ArrayAdapter对象

        List<Tb_singlelist> tb_singlelists = singleListDAO.getScrollData(0,
                (int) singleListDAO.getCount());
        strInfos = new String[tb_singlelists.size()];// 设置字符串数组的长度
        int m = 0;// 定义一个开始标识
        for (Tb_singlelist tb_singlelist : tb_singlelists) {// 遍历List泛型集合
            if (tb_singlelist.getLoveid()==Integer.parseInt(loveid))
            {
                strInfos[m] = tb_singlelist.getid()+" "+musicListDAO.find(tb_singlelist.getMusicid()).getName();
            }
            m++;// 标识加1
        }
        // 使用字符串数组初始化ArrayAdapter对象
        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strInfos);
        listView3.setAdapter(arrayAdapter);// 为ListView列表设置数据源
    }
}
